package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.ThirdPartyInterconnection;
import cn.stylefeng.guns.modular.gridsystem.mapper.ThirdPartyInterconnectionMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.ThirdPartyInterconnectionParam;
import cn.stylefeng.guns.modular.gridsystem.service.ThirdPartyInterconnectionService;
import cn.stylefeng.guns.sys.modular.org.service.SysOrgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 第三方对接 服务实现类
 *
 * @author ssy
 * @date 2021/11/04 17:42
 */
@Service
public class ThirdPartyInterconnectionServiceImpl extends ServiceImpl<ThirdPartyInterconnectionMapper, ThirdPartyInterconnection> implements ThirdPartyInterconnectionService {

    @Resource
    private SysOrgService sysOrgService;

    @Override
    public PageResult<ThirdPartyInterconnection> page(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {

        // 构造条件
        LambdaQueryWrapper<ThirdPartyInterconnection> queryWrapper = new LambdaQueryWrapper<>();

        // 拼接查询条件-
        if (ObjectUtil.isNotNull(thirdPartyInterconnectionParam) ) {
            if(ObjectUtil.isNotEmpty(thirdPartyInterconnectionParam.getOrgId())){
                List<Long> childIdListWithSelfById = sysOrgService.getChildIdListWithSelfById(thirdPartyInterconnectionParam.getOrgId());
                queryWrapper.in(ThirdPartyInterconnection::getOrgId, childIdListWithSelfById);
            }
        }

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ThirdPartyInterconnection> list(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {

        // 构造条件
        LambdaQueryWrapper<ThirdPartyInterconnection> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(thirdPartyInterconnectionParam)){
            if(ObjectUtil.isNotEmpty(thirdPartyInterconnectionParam.getOrgId())){
                queryWrapper.eq(ThirdPartyInterconnection::getOrgId,thirdPartyInterconnectionParam.getOrgId());
                queryWrapper.eq(ThirdPartyInterconnection::getTypeCode,"1");//1告警数据 2实时数据
            }
        }

        return this.list(queryWrapper);
    }

    @Override
    public void add(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {

        // 将dto转为实体
        ThirdPartyInterconnection thirdPartyInterconnection = new ThirdPartyInterconnection();
        BeanUtil.copyProperties(thirdPartyInterconnectionParam, thirdPartyInterconnection);
        Date date = new Date();
        long createTime = date.getTime();
        thirdPartyInterconnection.setCreateTime(date);
        String thirdPartyUrl = thirdPartyInterconnectionParam.getThirdPartyUrl();
        Long orgId = thirdPartyInterconnectionParam.getOrgId();
        //sha1加密生成appKey
        String sha1Hex = DigestUtils.sha1Hex(createTime + thirdPartyUrl + orgId);
        thirdPartyInterconnection.setAppKey(sha1Hex);
        this.save(thirdPartyInterconnection);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        this.removeById(thirdPartyInterconnectionParam.getId());
    }

    @Override
    public void edit(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {

        // 根据id查询实体
        ThirdPartyInterconnection thirdPartyInterconnection = this.queryThirdPartyInterconnection(thirdPartyInterconnectionParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(thirdPartyInterconnectionParam, thirdPartyInterconnection);

        this.updateById(thirdPartyInterconnection);
    }

    @Override
    public ThirdPartyInterconnection detail(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        return this.queryThirdPartyInterconnection(thirdPartyInterconnectionParam);
    }

    @Override
    public ThirdPartyInterconnection getByAppKey(String appKey) {
        QueryWrapper<ThirdPartyInterconnection> wrapper = new QueryWrapper<>();
        wrapper.eq("app_key",appKey);
        return this.getOne(wrapper);
    }


    /**
     * 获取第三方对接
     *
     * @author ssy
     * @since 2021/11/04 17:42
     */
    private ThirdPartyInterconnection queryThirdPartyInterconnection(ThirdPartyInterconnectionParam thirdPartyInterconnectionParam) {
        ThirdPartyInterconnection thirdPartyInterconnection = this.getById(thirdPartyInterconnectionParam.getId());
        return thirdPartyInterconnection;
    }
}
